package com.itcam.cm.manager.service.impl;

import com.itcam.cm.manager.mapper.SysMenuMapper;
import com.itcam.cm.manager.service.SysMenuService;
import com.itcam.cm.model.dto.system.SysMenuVo;
import com.itcam.cm.model.entity.system.SysMenu;
import com.itcam.cm.model.entity.system.SysUser;
import com.itcam.cm.model.entity.system.SysUserThreadLocalAuthContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    SysMenuMapper sysMenuMapper;

    @Override
    public List<SysMenu> findNodes() {

        // 获得菜单数据
        List<SysMenu> sysMenus = sysMenuMapper.selectAll();

        // 将未分组的数据进行分组处理
        List<SysMenu> tree = MenuHelper.buildTree(sysMenus);

        return tree;
    }

    @Override
    public void saveSysMenu(SysMenu sysMenu) {
        sysMenuMapper.insertSysMenu(sysMenu);
    }

    @Override
    public void updateSysMenu(SysMenu sysMenu) {
        sysMenuMapper.updateSysMenu(sysMenu);
    }

    @Override
    public List<SysMenuVo> menus() {
        SysUser sysUser = SysUserThreadLocalAuthContextUtil.get();

        // 查询数据库中菜单
        List<SysMenu> sysMenus = sysMenuMapper.selectSysMenusByUserId(sysUser.getId());
        List<SysMenu> sysMenusTree = MenuHelper.buildTree(sysMenus);

        // 转化成前端菜单
        List<SysMenuVo> sysMenuVos = MenuHelper.buildMenuVos(sysMenusTree);

        return sysMenuVos;
    }
}
